<?php

class ObservationController extends Controller
{
    public $layout='//layouts/body';
    public $pageTitle = '课堂观测平台';
    public $debug = false;
    public function init(){
        //Yii::app()->session['observation_id'] = '';exit;
    }
    public function actionIndex(){
        $app_info = CityApp::model()->findByPk(1);
        $user = '';
        if($app_info->is_type==1){//web类型
            $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
        }elseif($app_info->is_type==2){//订阅号
            $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
        }elseif($app_info->is_type==3){//服务号
            $openid = XUtils::isBasicAuth($app_info->wx_app_id,$app_info->wx_app_secret);
        }
        $observation_id = Yii::app()->session['observation_id'];
        if($observation_id){
            $this->redirect($this->createUrl('list',array('openid'=>$openid)));
        }else{
            $people_info = Human::model()->find('openid=:openid and is_login=:is_login',
                array(':openid'=>$openid,':is_login'=>1));
            if(!$people_info){
                //2表示专家
                $this->render('login',array('openid'=>$openid,'user'=>$user));
                exit();
            }else{
//                $this->render('login',array('openid'=>$openid,'user'=>$user));
                //存在
                if($people_info->role = 2){//老师
                    Yii::app()->session['observation_id'] = '1|'.$people_info->id;
                }else{//专家
                    Yii::app()->session['observation_id'] = '2|'.$people_info->id;
                }
                //1表示老师
                $this->redirect($this->createUrl('list',array('openid'=>$openid)));
            }
        }
    }
    public function actionAjaxLogin(){
        if(Yii::app()->request->isAjaxRequest){
            $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
            $key_sn = Yii::app()->request->getParam('key_sn');
            $people_info = Human::model()->find('role=:role and key_sn=:key_sn',
                array(':role'=>3,':key_sn'=>$key_sn));
            if(!$people_info){
                $data['status'] = 0;
                $data['message'] = '编号错误!';
                echo json_encode($data);
                exit();
            }
            Human::model()->updateAll(array('is_login'=>''),'openid=:openid',array(':openid'=>$openid));
            $people_info->openid = $openid;
            $people_info->is_login = 1;
            $people_info->update();
            $data['status'] = 1;
            $data['message'] = '登录成功';
            Yii::app()->session['observation_id'] = '2|'.$people_info->id;
            echo json_encode($data);
            exit();
        }
    }
    public function actionAjaxLogin1(){
        if(Yii::app()->request->isAjaxRequest){
            $code_num = Yii::app()->request->getParam('code_num');
            $user_name = Yii::app()->request->getParam('user_name');
            $passwd = sha1(Yii::app()->request->getParam('passwd'));
            $openid = Yii::app()->request->getParam('openid');
            $role = 2;
            $p_info = People::model()->find('role=:role and user_name=:user_name and code_num=:code_num',array(
                ':role'=>3,
                ':user_name'=>$user_name,
                ':code_num'=>$code_num
            ));
            $people_info = Human::model()->find('role=:role and key_sn=:key_sn',
                array(':role'=>$role,':key_sn'=>$code_num));
            Human::model()->updateAll(array('is_login'=>''),'openid=:openid',array(':openid'=>$openid));
            if($people_info){//human表存在
                if($p_info->passwd != $passwd){
                    $data['status'] = 0;
                    $data['message'] = '密码错误!';
                    echo json_encode($data);
                    exit();
                }
                $people_info->is_login = 1;
                $people_info->openid = $openid;
                $people_info->update();
                $data['status'] = 1;
                $data['message'] = '登录成功';
                Yii::app()->session['observation_id'] = '1|'.$people_info->pid;
                echo json_encode($data);
                exit();
            }else{//不存在  去people 表查询
                //查询people表
                if($p_info){
                    //密码是否正确？
                    if($p_info->passwd != $passwd){
                        $data['status'] = 0;
                        $data['message'] = '密码错误';
                        echo json_encode($data);
                        exit();
                    }
                    $db = new Human;
                    $db->user_name = $user_name;
                    $db->openid = $openid;
                    $db->key_sn = $code_num;
                    $db->role = $role;
                    $db->gid = $p_info->gid;
                    $db->cid = $p_info->cid;
                    $db->is_login = 1;
                    $db->passwd = $passwd;
                    $db->pid = $p_info->id;
                    $ok = $db->save();
                    if($ok){
                        $idd = $db->attributes['id'];
                        $data['status'] = 1;
                        $data['message'] = '登录成功';
                        Yii::app()->session['observation_id'] = '1|'.$p_info->id;
                        echo json_encode($data);
                        exit();
                    }else{
                        $data['status'] = 0;
                        $data['message'] = '登录失败';
                        echo json_encode($data);
                        exit();
                    }
                }else{//不存在
                    $data['status'] = 0;
                    $data['message'] = '不存在此老师';
                    echo json_encode($data);
                    exit();
                }
            }
        }
    }
    public function actionList(){
        $app_info = CityApp::model()->findByPk(1);
        $observation_id = Yii::app()->session['observation_id'];
        if($app_info->is_type==1){//web类型
            $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
        }elseif($app_info->is_type==2){//订阅号
            $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
        }elseif($app_info->is_type==3){//服务号
            $openid = XUtils::isBasicAuth($app_info->wx_app_id,$app_info->wx_app_secret);
        }
        $user = '';
        if(!$observation_id){
            $people_info = Human::model()->find('openid=:openid and is_login=:is_login',
                array(':openid'=>$openid,':is_login'=>1));
            if(!$people_info){
                //2表示专家
                $this->render('login',array('openid'=>$openid,'user'=>$user));
                exit();
            }else{
                //存在
                if($people_info->role = 2){//老师
                    Yii::app()->session['observation_id'] = '1|'.$people_info->id;
                }else{//专家
                    Yii::app()->session['observation_id'] = '2|'.$people_info->id;
                }
            }
        }
        $key = intval(Yii::app()->request->getParam('key'));
        if(!$key){
            $key = 1;
        }
        $db = Yii::app()->db->createCommand();
        $db->select('e.*,p.user_name');
        $db->from('{{open}} e');
        $db->leftJoin('{{people}} p','e.tid = p.id');
        if($key==1){
            $db->andWhere('e.status=:status OR e.status=:status1',array(':status'=>1,':status1'=>0));
        }else{
            $db->andWhere('e.status=:status',array(':status'=>2));
        }
        $first = 0;
        $total = Yii::app()->params['listpage'];;
        $db->order('create_time desc');
        $db->limit($total,$first);
        $list = $db->queryAll();
        $observation_id = Yii::app()->session['observation_id'];
        if($observation_id){
            $array = explode("|",$observation_id);

            if($list){
                foreach($list as $k=>$v){
                    $ctype = '';
                    $tid = $v['tid'];
                    $gid = explode(",",$v['gid']);
                    $zid = explode(",",$v['zid']);
                    $lid = explode(",",$v['lid']);
                    if(intval($array[0])==1){//老师
                        $lsid = $array[1];
                        if($lsid==$tid){
                            $ctype .= 'ls ';
                        }
                        if(in_array($lsid,$gid)){//观察者
                            $ctype .= 'gc ';
                        }
                        if(in_array($lsid,$lid)){//听课老师
                            $ctype .= 'tk ';
                        }
                    }elseif(intval($array[0])==2){//专家
                        $zjid = $array[1];
                        if (in_array($zjid,$zid)){
                            $ctype .= 'zj';
                        }
                    }
                    $list[$k]['ctype'] = trim($ctype,' ');
                }
            }
        }
        $this->render('list',array('openid'=>$openid,'list'=>$list,'array'=>$array));
    }
    public function actionLogOut(){
        $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
        Yii::app()->session['observation_id'] = '';
        Human::model()->updateAll(array('openid'=>'','is_login'=>''),'openid=:openid',array(':openid'=>$openid));
        $data['status'] = 1;
        $data['message']='退出成功';
        echo json_encode($data);
        exit();
    }

    public function actionAjaxOpen(){
        $this->layout = '//layouts/null';
        $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
        $key = intval(Yii::app()->request->getParam('key'));
        $page = intval(Yii::app()->request->getParam('page'));
        $db = Yii::app()->db->createCommand();
        $db->select('e.*,p.user_name');
        $db->from('{{open}} e');
        $db->leftJoin('{{people}} p','e.tid = p.id');
        if($key==1){
            $db->andWhere('e.status=:status OR e.status=:status1',array(':status'=>1,':status1'=>0));
        }else{
            $db->andWhere('e.status=:status',array(':status'=>2));
        }
        $first = $page*Yii::app()->params['listpage'];;;
        $total = Yii::app()->params['listpage'];;
        $db->order('create_time desc');
        $db->limit($total,$first);
        $list = $db->queryAll();
        if($list){
            $observation_id = Yii::app()->session['observation_id'];
            if($observation_id){
                $array = explode("|",$observation_id);
                foreach($list as $k=>$v){
                    $ctype = '';
                    $tid = $v['tid'];
                    $gid = explode(",",$v['gid']);
                    $zid = explode(",",$v['zid']);
                    $lid = explode(",",$v['lid']);
                    if(intval($array[0])==1){//老师
                        $lsid = $array[1];
                        if($lsid==$tid){
                            $ctype .= 'ls ';
                        }
                        if(in_array($lsid,$gid)){//观察者
                            $ctype .= 'gc ';
                        }
                        if(in_array($lsid,$lid)){//听课老师
                            $ctype .= 'tk ';
                        }
                    }elseif(intval($array[0])==2){//专家
                        $zjid = $array[1];
                        if (in_array($zjid,$zid)){
                            $ctype .= 'zj';
                        }
                    }
                    $list[$k]['ctype'] = trim($ctype,' ');
                }
            }
        }
        $this->render('open_list',array('openid'=>$openid,'list'=>$list));
    }



    public function actionAjaxMsg(){
        $oid = intval(Yii::app()->request->getParam('oid'));//openid
        $open_info = Open::model()->findByPk($oid);
        if($open_info){
            if($open_info->status==1){
                $data['status'] = 1;
                $data['message']='';
                echo json_encode($data);
                exit();
            }else{
                $data['status']=0;
                $data['message']='';
                echo json_encode($data);
                exit();
            }
        }else{
            $data['status']=0;
            $data['message']='';
            echo json_encode($data);
            exit();
        }
    }
    public function actionAjaxMsg1(){
        $oid = intval(Yii::app()->request->getParam('oid'));//openid
        $open_info = Open::model()->findByPk($oid);
        if($open_info){
            if($open_info->status==2){
                $data['status'] = 1;
                $data['message']='';
                echo json_encode($data);
                exit();
            }else{
                $data['status']=0;
                $data['message']='';
                echo json_encode($data);
                exit();
            }
        }else{
            $data['status']=0;
            $data['message']='';
            echo json_encode($data);
            exit();
        }
    }
    public function actionAjaxSignIn(){
        $oid = Yii::app()->request->getParam('oid');
        $uid = Yii::app()->request->getParam('uid');
        $type =  Yii::app()->request->getParam('type');
        $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$oid,':uid'=>$uid,':type'=>$type));
        if($signIn){
            $data['status'] = 1;
            $data['message'] = '签到成功';
            echo json_encode($data);
            exit();
        }else{
            $db = new OpenSignin();
            $db->oid = $oid;
            $db->uid = $uid;
            $db->type = $type;
            $db->create_time = time();
            $ok = $db->save();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '签到成功';
                echo json_encode($data);
                exit();
            }else{
                $data['status'] = 0;
                $data['message'] = '签到失败';
                echo json_encode($data);
                exit();
            }
        }
    }

    public function actionShow(){
        $app_info = CityApp::model()->findByPk(1);
        $user = '';
        if($app_info->is_type==1){//web类型
            $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
        }elseif($app_info->is_type==2){//订阅号
            $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
        }elseif($app_info->is_type==3){//服务号
            $openid = XUtils::isBasicAuth($app_info->wx_app_id,$app_info->wx_app_secret);
        }
        //1  session 记录
        $observation_id = Yii::app()->session['observation_id'];
        if(!$observation_id){
            $people_info = Human::model()->find('openid=:openid and is_login=:is_login',
                array(':openid'=>$openid,':is_login'=>1));
            if($people_info){
                //存在
                if($people_info->role = 2){//老师
                    Yii::app()->session['observation_id'] = '1|'.$people_info->id;
                }else{//专家
                    Yii::app()->session['observation_id'] = '2|'.$people_info->id;
                }
                $observation_id = Yii::app()->session['observation_id'];
            }else{
                //2表示专家
                $this->render('login',array('openid'=>$openid,'user'=>$user));
                exit();
            }
        }
        $array = explode("|",$observation_id);
        $id = intval(Yii::app()->request->getParam('id'));
        $open_info = Open::model()->findByPk($id);
        if($open_info->status == 0){
            if($open_info->start_time < time()){
                $open_info->status = 1;
                $open_info->update();
            }
        }
        if($open_info->status == 1){
            if($open_info->end_time < time()){
                $open_info->status = 2;
                $open_info->update();
            }
        }


        $data['type'] = '';
        $tid = $open_info->tid;
        $gid = explode(",",$open_info->gid);
        $zid = explode(",",$open_info->zid);
        $lid = explode(",",$open_info->lid);
        if(intval($array[0])==1){//老师
            $lsid = $array[1];
            if($lsid==$tid){
                $data['type'] .= 'ls ';
            }
            if(in_array($lsid,$gid)){//观察者
                $data['type'] .= 'gc ';
            }
            if(in_array($lsid,$lid)){//听课老师
                $data['type'] .= 'tk ';
            }
        }elseif(intval($array[0])==2){//专家
            if($zid[0] !=''){
                $zjid = $array[1];
                if (in_array($zjid,$zid)){
                    $data['type'] .= 'zj';
                }
            }else{
                $data['type'] .= 'zj';
            }
        }
        $data['type']  = trim($data['type'],' ');
        if($data['type']){
            $aaa = explode(" ",$data['type']);
        }else{
            $aaa = '';
        }

        $role = Yii::app()->request->getParam('Crole');
        if($open_info->status==0){   //未开始
            if(in_array("gc", $aaa) || in_array("tk", $aaa)){
                if(!$role){
                    if(in_array("gc", $aaa)){
                        $this->render('sign_tj',array('aaa'=>$aaa,'zid'=>$zid,'lid'=>$lid,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                            'type'=>'gc',
                            'uid'=>intval($array[1]),
                        ));
                        exit();
                    }
                    if(in_array("tk", $aaa)){
                        //是否签到
                        $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'tk'));
                        $this->render('no_start',array('aaa'=>$aaa,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                            'signIn'=>$signIn,
                            'type'=>'tk',
                            'uid'=>intval($array[1]),
                        ));
                        exit();
                    }
                }else{
                    if(in_array("tk", $aaa)){
                        //是否签到
                        $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'tk'));
                        $this->render('no_start',array('aaa'=>$aaa,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                            'signIn'=>$signIn,
                            'type'=>'tk',
                            'uid'=>intval($array[1]),
                        ));
                        exit();
                    }
                    if(in_array("gc", $aaa)){
                        $this->render('sign_tj',array('aaa'=>$aaa,'zid'=>$zid,'lid'=>$lid,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                            'type'=>'gc',
                            'uid'=>intval($array[1]),
                        ));
                        exit();
                    }
                }
            }elseif(in_array("ls", $aaa)){
                $tid = $open_info->tid;
                $human_info = Human::model()->find('pid=:pid',array(':pid'=>$tid));
                if($human_info){
                    $db1 = Yii::app()->db->createCommand();
                    $db1->select('*');
                    $db1->from('{{people}}');
                    $db1->andWhere('cid=:cid and role=:role',array(':cid'=>$human_info->cid,':role'=>1));
                    $db1->order('zimu asc,id desc');
                    $p_info = $db1->queryAll();
                }else{
                    $p_info = '';
                }
                $db = Yii::app()->db->createCommand();
                $db->select('*');
                $db->from('{{open}}');
                $db->andWhere('tid=:tid and id !=:id',array(':tid'=>$tid,':id'=>$open_info->id));
                $db->order('create_time desc');
                $db->limit(10);
                $open_array = $db->queryAll();
                $this->render('zuowei',array('p_info'=>$p_info,'open_array'=>$open_array,'zid'=>$zid,'lid'=>$lid,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                    'type'=>'ls',
                    'uid'=>intval($array[1]),
                ));
            }elseif(in_array("zj", $aaa)){
                //是否签到
                $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'zj'));
                $this->render('no_start',array('aaa'=>$aaa,'open_info'=>$open_info,'openid'=>$openid,
                    'id'=>$id,
                    'signIn'=>$signIn,
                    'type'=>'zj',
                    'uid'=>intval($array[1]),
                ));
            }
        }elseif($open_info->status==1){//正在进行中
            if(in_array("gc", $aaa) || in_array("tk", $aaa)){
                if(!$role){
                    if(in_array("gc", $aaa)){
                        $types = intval(Yii::app()->request->getParam('type'));
                        $openRunning = OpenRunning::model()->findAll('oid=:oid',array(':oid'=>$id));
                        $this->render('sign_tj1',array('openRunning'=>$openRunning,'zid'=>$zid,'lid'=>$lid,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                            'aaa'=>$aaa,
                            'type'=>'gc',
                            'types'=>$types,
                            'uid'=>intval($array[1]),
                        ));
                        exit();
                    }
                    if(in_array("tk", $aaa)){
                        //是否签到
                        $os = OpenSignin::model()->find('oid=:oid and type=:type and uid=:uid',array(':oid'=>$id,':type'=>'tk',':uid'=>intval($array[1])));
                        if($os){
                            //获取评星表
                            $openPoint = OpenPoint::model()->findAll('pid=:pid',array(':pid'=>0));
                            $criteria=new CDbCriteria;
                            $criteria->condition='pid !=:pid';
                            $criteria->params= array(':pid'=>0);
                            $criteria->order = 'listorder desc,id asc';

                            $allChildPoint = OpenPoint::model()->findAll($criteria);

                            //start
                            $startInfo = OpenStart::model()->findAll('oid=:oid and type=:type and uid=:uid'
                                ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            $startArr = array();
                            if($startInfo){
                                foreach($startInfo as $zzx){
                                    $startArr[$zzx->opid] = $zzx->start;
                                }
                            }
                            //comment
                            $commentInfo = OpenComment::model()->find('oid=:oid and type=:type and uid=:uid' ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            //是否签到
                            $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'tk'));
                            $this->render('recommend',array('commentInfo'=>$commentInfo,'startInfo'=>$startArr,'allChildPoint'=>$allChildPoint,'openPoint'=>$openPoint,'open_info'=>$open_info,'openid'=>$openid,
                                'id'=>$id,
                                'aaa'=>$aaa,
                                'signIn'=>$signIn,
                                'type'=>'tk',
                                'uid'=>intval($array[1]),
                            ));
                            exit();
                        }else{
                            //是否签到
                            $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'tk'));
                            $this->render('no_start',array('open_info'=>$open_info,'openid'=>$openid,
                                'id'=>$id,
                                'aaa'=>$aaa,
                                'signIn'=>$signIn,
                                'type'=>'tk',
                                'uid'=>intval($array[1]),
                            ));
                            exit();
                        }
                    }
                }else{
                    if(in_array("tk", $aaa)){
                        //是否签到
                        $os = OpenSignin::model()->find('oid=:oid and type=:type and uid=:uid',array(':oid'=>$id,':type'=>'tk',':uid'=>intval($array[1])));
                        if($os){
                            //获取评星表
                            $openPoint = OpenPoint::model()->findAll('pid=:pid',array(':pid'=>0));
                            $criteria=new CDbCriteria;
                            $criteria->condition='pid !=:pid';
                            $criteria->params= array(':pid'=>0);
                            $criteria->order = 'listorder desc,id asc';

                            $allChildPoint = OpenPoint::model()->findAll($criteria);

                            //start
                            $startInfo = OpenStart::model()->findAll('oid=:oid and type=:type and uid=:uid'
                                ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            $startArr = array();
                            if($startInfo){
                                foreach($startInfo as $zzx){
                                    $startArr[$zzx->opid] = $zzx->start;
                                }
                            }
                            //comment
                            $commentInfo = OpenComment::model()->find('oid=:oid and type=:type and uid=:uid' ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            //是否签到
                            $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'tk'));
                            $this->render('recommend',array('commentInfo'=>$commentInfo,'startInfo'=>$startArr,'allChildPoint'=>$allChildPoint,'openPoint'=>$openPoint,'open_info'=>$open_info,'openid'=>$openid,
                                'id'=>$id,
                                'aaa'=>$aaa,
                                'signIn'=>$signIn,
                                'type'=>'tk',
                                'uid'=>intval($array[1]),
                            ));
                            exit();
                        }else{
                            //是否签到
                            $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'tk'));
                            $this->render('no_start',array('open_info'=>$open_info,'openid'=>$openid,
                                'id'=>$id,
                                'aaa'=>$aaa,
                                'signIn'=>$signIn,
                                'type'=>'tk',
                                'uid'=>intval($array[1]),
                            ));
                            exit();
                        }
                    }
                    if(in_array("gc", $aaa)){
                        $types = intval(Yii::app()->request->getParam('type'));
                        $openRunning = OpenRunning::model()->findAll('oid=:oid',array(':oid'=>$id));
                        $this->render('sign_tj1',array('openRunning'=>$openRunning,'zid'=>$zid,'lid'=>$lid,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                            'aaa'=>$aaa,
                            'type'=>'gc',
                            'types'=>$types,
                            'uid'=>intval($array[1]),
                        ));
                        exit();
                    }
                }
            }elseif(in_array("ls", $aaa)){
                $this->render('zuowei1',array('zid'=>$zid,'lid'=>$lid,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                    'type'=>'ls',
                    'uid'=>intval($array[1]),
                ));
                exit();
            }elseif(in_array("zj", $aaa)) {
                //是否签到
                $os = OpenSignin::model()->find('oid=:oid and type=:type and uid=:uid',array(':oid'=>$id,':type'=>'zj',':uid'=>intval($array[1])));
                if($os){
                    //获取评星表
                    $openPoint = OpenPoint::model()->findAll('pid=:pid',array(':pid'=>0));
                    $criteria=new CDbCriteria;
                    $criteria->condition='pid !=:pid';
                    $criteria->params= array(':pid'=>0);
                    $criteria->order = 'listorder desc,id asc';

                    $allChildPoint = OpenPoint::model()->findAll($criteria);

                    //start
                    $startInfo = OpenStart::model()->findAll('oid=:oid and type=:type and uid=:uid'
                        ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                    $startArr = array();
                    if($startInfo){
                        foreach($startInfo as $zzx){
                            $startArr[$zzx->opid] = $zzx->start;
                        }
                    }
                    //comment
                    $commentInfo = OpenComment::model()->find('oid=:oid and type=:type and uid=:uid' ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                    //是否签到
                    $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'zj'));
                    $this->render('recommend',array('commentInfo'=>$commentInfo,'startInfo'=>$startArr,'allChildPoint'=>$allChildPoint,'openPoint'=>$openPoint,'open_info'=>$open_info,'openid'=>$openid,
                        'id'=>$id,
                        'aaa'=>$aaa,
                        'signIn'=>$signIn,
                        'type'=>'zj',
                        'uid'=>intval($array[1]),
                    ));
                    exit();
                }else{
                    //是否签到
                    $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'zj'));
                    $this->render('no_start',array('open_info'=>$open_info,'openid'=>$openid,
                        'id'=>$id,
                        'aaa'=>$aaa,
                        'signIn'=>$signIn,
                        'type'=>'zj',
                        'uid'=>intval($array[1]),
                    ));
                    exit();
                }
            }
        }else{//已经结束
            if($open_info->status==2 && time() > $open_info->end_time+(30*60)){
                $this->render('zuowei1',array('zid'=>$zid,'lid'=>$lid,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                    'type'=>'tk',
                    'uid'=>intval($array[1]),
                ));
                exit();
            }else{
                if(in_array("gc", $aaa) || in_array("tk", $aaa)){
                    if(!$role){
                        if(in_array("gc", $aaa)){
                            //获取评星表
                            $openPoint = OpenPoint::model()->findAll('pid=:pid',array(':pid'=>0));
                            $criteria=new CDbCriteria;
                            $criteria->condition='pid !=:pid';
                            $criteria->params= array(':pid'=>0);
                            $criteria->order = 'listorder desc,id asc';
                            $allChildPoint = OpenPoint::model()->findAll($criteria);
                            //start
                            $startInfo = OpenStart::model()->findAll('oid=:oid and type=:type and uid=:uid'
                                ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            $startArr = array();
                            if($startInfo){
                                foreach($startInfo as $zzx){
                                    $startArr[$zzx->opid] = $zzx->start;
                                }
                            }
                            //comment
                            $commentInfo = OpenComment::model()->find('oid=:oid and type=:type and uid=:uid' ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            //是否签到
                            $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'gc'));
                            $this->render('recommend',array('commentInfo'=>$commentInfo,'startInfo'=>$startArr,'allChildPoint'=>$allChildPoint,'openPoint'=>$openPoint,'open_info'=>$open_info,'openid'=>$openid,
                                'signIn'=>$signIn,
                                'type'=>'gc',
                                'uid'=>intval($array[1]),
                            ));
                            exit();
                        }
                        if(in_array("tk", $aaa)){
//获取评星表
                            $openPoint = OpenPoint::model()->findAll('pid=:pid',array(':pid'=>0));
                            $criteria=new CDbCriteria;
                            $criteria->condition='pid !=:pid';
                            $criteria->params= array(':pid'=>0);
                            $criteria->order = 'listorder desc,id asc';
                            $allChildPoint = OpenPoint::model()->findAll($criteria);
                            //start
                            $startInfo = OpenStart::model()->findAll('oid=:oid and type=:type and uid=:uid'
                                ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            $startArr = array();
                            if($startInfo){
                                foreach($startInfo as $zzx){
                                    $startArr[$zzx->opid] = $zzx->start;
                                }
                            }
                            //comment
                            $commentInfo = OpenComment::model()->find('oid=:oid and type=:type and uid=:uid' ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            //是否签到
                            $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'tk'));
                            $this->render('recommend',array('commentInfo'=>$commentInfo,'startInfo'=>$startArr,'allChildPoint'=>$allChildPoint,'openPoint'=>$openPoint,'open_info'=>$open_info,'openid'=>$openid,
                                'signIn'=>$signIn,
                                'type'=>'tk',
                                'uid'=>intval($array[1]),
                            ));
                            exit();
                        }
                    }else{
                        if(in_array("tk", $aaa)){
                            //获取评星表
                            $openPoint = OpenPoint::model()->findAll('pid=:pid',array(':pid'=>0));
                            $criteria=new CDbCriteria;
                            $criteria->condition='pid !=:pid';
                            $criteria->params= array(':pid'=>0);
                            $criteria->order = 'listorder desc,id asc';
                            $allChildPoint = OpenPoint::model()->findAll($criteria);
                            //start
                            $startInfo = OpenStart::model()->findAll('oid=:oid and type=:type and uid=:uid'
                                ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            $startArr = array();
                            if($startInfo){
                                foreach($startInfo as $zzx){
                                    $startArr[$zzx->opid] = $zzx->start;
                                }
                            }
                            //comment
                            $commentInfo = OpenComment::model()->find('oid=:oid and type=:type and uid=:uid' ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            //是否签到
                            $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'tk'));
                            $this->render('recommend',array('commentInfo'=>$commentInfo,'startInfo'=>$startArr,'allChildPoint'=>$allChildPoint,'openPoint'=>$openPoint,'open_info'=>$open_info,'openid'=>$openid,
                                'signIn'=>$signIn,
                                'type'=>'tk',
                                'uid'=>intval($array[1]),
                            ));
                            exit();
                        }
                        if(in_array("gc", $aaa)){
                            //获取评星表
                            $openPoint = OpenPoint::model()->findAll('pid=:pid',array(':pid'=>0));
                            $criteria=new CDbCriteria;
                            $criteria->condition='pid !=:pid';
                            $criteria->params= array(':pid'=>0);
                            $criteria->order = 'listorder desc,id asc';

                            $allChildPoint = OpenPoint::model()->findAll($criteria);

                            //start
                            $startInfo = OpenStart::model()->findAll('oid=:oid and type=:type and uid=:uid'
                                ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            $startArr = array();
                            if($startInfo){
                                foreach($startInfo as $zzx){
                                    $startArr[$zzx->opid] = $zzx->start;
                                }
                            }
                            //comment
                            $commentInfo = OpenComment::model()->find('oid=:oid and type=:type and uid=:uid' ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                            //是否签到
                            $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'gc'));
                            $this->render('recommend',array('commentInfo'=>$commentInfo,'startInfo'=>$startArr,'allChildPoint'=>$allChildPoint,'openPoint'=>$openPoint,'open_info'=>$open_info,'openid'=>$openid,
                                'signIn'=>$signIn,
                                'type'=>'gc',
                                'uid'=>intval($array[1]),
                            ));
                            exit();
                        }
                    }
                }elseif(in_array("ls", $aaa)){
                    $this->render('zuowei1',array('zid'=>$zid,'lid'=>$lid,'id'=>$id,'open_info'=>$open_info,'openid'=>$openid,
                        'type'=>'ls',
                        'uid'=>intval($array[1]),
                    ));
                    exit();
                }elseif(in_array("zj", $aaa)) {
                    //获取评星表
                    $openPoint = OpenPoint::model()->findAll('pid=:pid',array(':pid'=>0));
                    $criteria=new CDbCriteria;
                    $criteria->condition='pid !=:pid';
                    $criteria->params= array(':pid'=>0);
                    $criteria->order = 'listorder desc,id asc';
                    $allChildPoint = OpenPoint::model()->findAll($criteria);
                    //start
                    $startInfo = OpenStart::model()->findAll('oid=:oid and type=:type and uid=:uid'
                        ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                    $startArr = array();
                    if($startInfo){
                        foreach($startInfo as $zzx){
                            $startArr[$zzx->opid] = $zzx->start;
                        }
                    }
                    //comment
                    $commentInfo = OpenComment::model()->find('oid=:oid and type=:type and uid=:uid' ,array(':oid'=>$open_info->id,':type'=>$array[0],':uid'=>$array[1]));
                    //是否签到
                    $signIn = OpenSignin::model()->find('oid=:oid and uid=:uid and type=:type',array(':oid'=>$id,':uid'=>intval($array[1]),':type'=>'zj'));
                    $this->render('recommend',array('commentInfo'=>$commentInfo,'startInfo'=>$startArr,'allChildPoint'=>$allChildPoint,'openPoint'=>$openPoint,'open_info'=>$open_info,'openid'=>$openid,
                        'signIn'=>$signIn,
                        'type'=>'zj',
                        'uid'=>intval($array[1]),
                    ));
                    exit();
                }
            }
        }
    }
    public function actionCreateOpen(){
        $techer = People::model()->findAll('role=:role',array(':role'=>3));
        $zhuanjia = Human::model()->findAll('role=:role',array(':role'=>3));
        $this->render('createOpen',array('techer'=>$techer,'zhuanjia'=>$zhuanjia));
    }
    public function actionAjaxCreateOpen(){
        $title = CHtml::encode(Yii::app()->request->getParam('title'));
        $room = CHtml::encode(Yii::app()->request->getParam('room'));
        $open_time = CHtml::encode(Yii::app()->request->getParam('open_time'))  ;
        $start_time = strtotime(Yii::app()->request->getParam('start_time'))  ;
        $end_time = strtotime(Yii::app()->request->getParam('end_time'))  ;
        $jk = intval(Yii::app()->request->getParam('jk'));
        if($end_time < $start_time){
            $data['status']=0;
            $data['message']='结束时间不得小于开始时间';
            echo json_encode($data);
            exit();
        }
        $observation_id = Yii::app()->session['observation_id'];
        if(!$observation_id){
            $data['status']=0;
            $data['message']='错误操作';
            echo json_encode($data);
            exit();
        }
        $tk = Yii::app()->request->getParam('tk')  ;
        $tks = explode("&",$tk);
        $tkstr = '';
        foreach($tks as $kkk=>$vvv){
            $ccc = explode("=",$vvv);
            $tkstr .= $ccc[1].',';
        }
        $tkstr = trim($tkstr,',');
        $gc = Yii::app()->request->getParam('gc')  ;
        $gcs = explode("&",$gc);
        $gcstr = '';
        foreach($gcs as $gckk=>$gcvv){
            $ccc = explode("=",$gcvv);
            $gcstr .= $ccc[1].',';
        }
        $gcstr = trim($gcstr,',');

        $db = new Open();
        $db->tid = $jk;
        $db->lid = $tkstr;
        $db->gid = $gcstr;
        $db->title = $title;
        $db->start_time = $start_time;
        $db->end_time = $end_time;
        $db->jiaoan = '';
        $db->info = '';
        $db->status = 1;
        $db->type = 1;//随堂课
        $db->open_time = $open_time;
        $db->create_time = time();
        $db->room = $room;
        $ok = $db->save();
        if($ok){
            $data['status']=1;
            $data['message']='创建成功';
            echo json_encode($data);
            exit();
        }else{
            $data['status']=1;
            $data['message']='创建失败';
            echo json_encode($data);
            exit();
        }
    }
    public function actionAccounting(){
        $key = Yii::app()->request->getParam('key');
        $app_info = CityApp::model()->findByPk(1);
        $user = '';

        if($key){
            $id = intval(Yii::app()->request->getParam('id'));
            $open_info = Open::model()->findByPk($id);
            //父类
            $openPoint = OpenPoint::model()->findAll('pid =:pid',array(':pid'=>0));
            $arr = array();
            $arr1 = array();
            $thisOpen = 0;
            $thisOpen1 = 0;
            foreach($openPoint as $kk=>$vv){
                $allDb = $vv->point;//大分类总权重
                $star = Yii::app()->db->createCommand("SELECT SUM(start) as start,COUNT(id) as count FROM {{open_start}}
WHERE oid=$open_info->id AND pid=$vv->id AND type=1")->queryRow();
                if($star){
                    if($star['count']){
                        $pingjun = $star['start']/$star['count'];
                        $cc = ($pingjun*$allDb)/5;
                        $sy = $allDb - $cc;
                    }else{
                        $cc = 0;
                        $sy = 0;
                    }
                }else{
                    $cc = 0;
                    $sy = 0;
                }
                $arr[$kk]['title'] = $vv->title;
                $arr[$kk]['sj']= $cc;
                $arr[$kk]['sy']= $sy;
                $thisOpen = $thisOpen+$cc;
            }

            foreach($openPoint as $kkk=>$vvv){
                $allDb = $vvv->point;//大分类总权重
                $star = Yii::app()->db->createCommand("SELECT SUM(start) as start,COUNT(id) as count FROM {{open_start}}
WHERE oid=$open_info->id AND pid=$vvv->id AND type=2")->queryRow();
                if($star){
                    if($star['count']){
                        $pingjun = $star['start']/$star['count'];
                        $cc = ($pingjun*$allDb)/5;
                        $sy = $allDb - $cc;
                    }else{
                        $cc = 0;
                        $sy = 0;
                    }
                }else{
                    $cc = 0;
                    $sy = 0;
                }
                $arr1[$kkk]['title'] = $vvv->title;
                $arr1[$kkk]['sj']= $cc;
                $arr1[$kkk]['sy']= $sy;
                $thisOpen1 = $thisOpen1+$cc;
            }
            $count = 0;
            if($thisOpen1 >0 && $thisOpen >0){
                $count = ($thisOpen1+$thisOpen)/2;
            }else{
                if($thisOpen1==0 && $thisOpen==0){
                    $count = 0;
                }else{
                    if($thisOpen1 ==0 && $thisOpen !=0){
                        $count = $thisOpen;
                    }elseif($thisOpen ==0 && $thisOpen1 !=0){
                        $count = $thisOpen1;
                    }
                }
            }
            $open_info->point = $count;
            $open_info->update();
            $thisOpen = $count;
            //统计历史
            $lishi = Yii::app()->db->createCommand("SELECT SUM(point) as point,COUNT(id) as count FROM {{open}}
WHERE id !=$open_info->id AND point !='' AND tid=$open_info->tid")->queryRow();

            $lishiData = Yii::app()->db->createCommand("SELECT * FROM {{open}}
WHERE tid=$open_info->tid AND point !=''")->queryAll();

            $huida_time = Yii::app()->db->createCommand("SELECT SUM(running) as count FROM {{open_running}}
WHERE oid =$open_info->id AND type=1 ")->queryRow();
            $huodong_time = Yii::app()->db->createCommand("SELECT SUM(running) as count FROM {{open_running}}
WHERE oid =$open_info->id AND type=2 ")->queryRow();
            $orderCount = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{open_order}}
WHERE oid =$open_info->id AND num !='' ")->queryRow();

            $criteria=new CDbCriteria;
            $criteria->condition='pid !=:pid';
            $criteria->params= array(':pid'=>0);
            $criteria->order = 'listorder desc,id asc';

            $allChildPoint = OpenPoint::model()->findAll($criteria);
            //start
            $startInfo = Yii::app()->db->createCommand("SELECT SUM(start) as count,COUNT(start) as num,opid FROM tbl_open_start WHERE oid=$open_info->id GROUP BY opid")->queryAll();;
            $startArr = array();
            if($startInfo){
                foreach($startInfo as $zzx){
                    $startArr[$zzx['opid']] = sprintf("%.2f", ($zzx['count']/$zzx['num']));
                }
            }

            $comment=  Yii::app()->db->createCommand("(SELECT t1.text,t1.create_time,t2.user_name,t1.type FROM tbl_open_comment t1 LEFT JOIN tbl_people t2 ON t1.uid=t2.id  WHERE oid=$open_info->id AND type=1) UNION (SELECT t3.text,t3.create_time,t4.user_name,t3.type FROM tbl_open_comment t3 LEFT JOIN tbl_human t4 ON t3.uid=t4.id  WHERE oid=$open_info->id AND type=2) ORDER BY create_time DESC")->queryAll();



            $this->render('accounting1',array(
                'comment'=>$comment,
                'allChildPoint'=>$allChildPoint,
                'startInfo'=>$startArr,
                'orderCount'=>$orderCount,
                'huida_time'=>$huida_time,
                'huodong_time'=>$huodong_time,
                'lishiData'=>$lishiData,
                'lishi'=>$lishi,
                'thisOpen'=>$thisOpen,
                'arr'=>$arr,
                'arr1'=>$arr1,
                'openid'=>'backend',
                'openPoint'=>$openPoint,
                'open_info'=>$open_info,
            ));
        }else{
            if($app_info->is_type==1){//web类型
                $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
            }elseif($app_info->is_type==2){//订阅号
                $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
            }elseif($app_info->is_type==3){//服务号
                $openid = XUtils::isBasicAuth($app_info->wx_app_id,$app_info->wx_app_secret);
            }
            //1  session 记录
            $observation_id = Yii::app()->session['observation_id'];
            if(!$observation_id){
                $people_info = Human::model()->find('openid=:openid and is_login=:is_login',
                    array(':openid'=>$openid,':is_login'=>1));
                if($people_info){
                    //存在
                    if($people_info->role = 2){//老师
                        Yii::app()->session['observation_id'] = '1|'.$people_info->id;
                    }else{//专家
                        Yii::app()->session['observation_id'] = '2|'.$people_info->id;
                    }
                    $observation_id = Yii::app()->session['observation_id'];
                }else{
                    //2表示专家
                    $this->render('login',array('openid'=>$openid,'user'=>$user));
                    exit();
                }
            }
            $array = explode("|",$observation_id);

            $id = intval(Yii::app()->request->getParam('id'));
            $open_info = Open::model()->findByPk($id);
            $data['type'] = '';
            $tid = $open_info->tid;
            $gid = explode(",",$open_info->gid);
            $zid = explode(",",$open_info->zid);
            $lid = explode(",",$open_info->lid);
            if(intval($array[0])==1){//老师
                $lsid = $array[1];
                if($lsid==$tid){
                    $data['type'] .= 'ls ';
                }
                if(in_array($lsid,$gid)){//观察者
                    $data['type'] .= 'gc ';
                }
                if(in_array($lsid,$lid)){//听课老师
                    $data['type'] .= 'tk ';
                }
            }elseif(intval($array[0])==2){//专家
                if($zid[0] !=''){
                    $zjid = $array[1];
                    if (in_array($zjid,$zid)){
                        $data['type'] .= 'zj';
                    }
                }else{
                    $data['type'] .= 'zj';
                }
            }
            $data['type']  = trim($data['type'],' ');
            if($data['type']){
                $aaa = explode(" ",$data['type']);
            }else{
                $aaa = '';
            }





            //父类
            $openPoint = OpenPoint::model()->findAll('pid =:pid',array(':pid'=>0));
            $arr = array();
            $arr1 = array();
            $thisOpen = 0;
            foreach($openPoint as $kk=>$vv){
                $allDb = $vv->point;//大分类总权重
                $star = Yii::app()->db->createCommand("SELECT SUM(start) as start,COUNT(id) as count FROM {{open_start}}
WHERE oid=$open_info->id AND pid=$vv->id AND type=1")->queryRow();
                if($star){
                    if($star['count']){
                        $pingjun = $star['start']/$star['count'];
                        $cc = ($pingjun*$allDb)/5;
                        $sy = $allDb - $cc;
                    }else{
                        $cc = 0;
                        $sy = 0;
                    }
                }else{
                    $cc = 0;
                    $sy = 0;
                }
                $arr[$kk]['sj']= $cc;
                $arr[$kk]['sy']= $sy;
                $thisOpen = $thisOpen+$cc;
            }
            foreach($openPoint as $kkk=>$vvv){
                $allDb = $vvv->point;//大分类总权重
                $star = Yii::app()->db->createCommand("SELECT SUM(start) as start,COUNT(id) as count FROM {{open_start}}
WHERE oid=$open_info->id AND pid=$vvv->id AND type=2")->queryRow();
                if($star){
                    if($star['count']){
                        $pingjun = $star['start']/$star['count'];
                        $cc = ($pingjun*$allDb)/5;
                        $sy = $allDb - $cc;
                    }else{
                        $cc = 0;
                        $sy = 0;
                    }
                }else{
                    $cc = 0;
                    $sy = 0;
                }
                $arr1[$kkk]['sj']= $cc;
                $arr1[$kkk]['sy']= $sy;
            }
            $open_info->point = $thisOpen;
            $open_info->update();
            //统计历史
            $lishi = Yii::app()->db->createCommand("SELECT SUM(point) as point,COUNT(id) as count FROM {{open}}
WHERE id !=$open_info->id AND point !='' AND tid=$open_info->tid")->queryRow();

            //历史纪录
            if(in_array("ls", $aaa)){
                $lishiData = Yii::app()->db->createCommand("SELECT * FROM {{open}}
WHERE tid=$open_info->tid AND point !=''")->queryAll();
            }else{
                $lishiData = '';
            }
            //<h5>回答时间总时长 ：<b style="color: darkred;">16分26秒</b> </h5>
            //<h5>活动时间总时长 ：<b style="color: darkred;">16分26秒</b></h5>
            $huida_time = Yii::app()->db->createCommand("SELECT SUM(running) as count FROM {{open_running}}
WHERE oid =$open_info->id AND type=1 ")->queryRow();
            $huodong_time = Yii::app()->db->createCommand("SELECT SUM(running) as count FROM {{open_running}}
WHERE oid =$open_info->id AND type=2 ")->queryRow();
            $orderCount = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{open_order}}
WHERE oid =$open_info->id AND num !='' ")->queryRow();

            $criteria=new CDbCriteria;
            $criteria->condition='pid !=:pid';
            $criteria->params= array(':pid'=>0);
            $criteria->order = 'listorder desc,id asc';

            $allChildPoint = OpenPoint::model()->findAll($criteria);
            //start
            $startInfo = Yii::app()->db->createCommand("SELECT SUM(start) as count,COUNT(start) as num,opid FROM tbl_open_start WHERE oid=$open_info->id GROUP BY opid")->queryAll();;
            $startArr = array();
            if($startInfo){
                foreach($startInfo as $zzx){
                    $startArr[$zzx['opid']] = sprintf("%.2f", ($zzx['count']/$zzx['num']));
                }
            }

            $comment=  Yii::app()->db->createCommand("(SELECT t1.text,t1.create_time,t2.user_name,t1.type FROM tbl_open_comment t1 LEFT JOIN tbl_people t2 ON t1.uid=t2.id  WHERE oid=$open_info->id AND type=1) UNION (SELECT t3.text,t3.create_time,t4.user_name,t3.type FROM tbl_open_comment t3 LEFT JOIN tbl_human t4 ON t3.uid=t4.id  WHERE oid=$open_info->id AND type=2) ORDER BY create_time DESC")->queryAll();
            if(in_array("ls", $aaa)){
                $tty = 'ls';

            }else{
                $tty = 'tk';
            }
            $this->render('accounting',array(
                'comment'=>$comment,
                'allChildPoint'=>$allChildPoint,
                'startInfo'=>$startArr,
                'orderCount'=>$orderCount,
                'huida_time'=>$huida_time,
                'huodong_time'=>$huodong_time,
                'lishiData'=>$lishiData,
                'lishi'=>$lishi,
                'thisOpen'=>$thisOpen,
                'arr'=>$arr,
                'arr1'=>$arr1,
                'openPoint'=>$openPoint,
                'open_info'=>$open_info,
                'openid'=>$openid,
                'type'=>$tty,
                'uid'=>intval($array[1]),
            ));
        }
    }
    public function actionCheckRole(){
        if(Yii::app()->request->isAjaxRequest){
            $observation_id = Yii::app()->session['observation_id'];
            $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
            if(!$observation_id){
                $people_info = Human::model()->find('openid=:openid and is_login=:is_login',
                    array(':openid'=>$openid,':is_login'=>1));
                if($people_info){
                    //存在
                    if($people_info->role = 2){//老师
                        Yii::app()->session['observation_id'] = '1|'.$people_info->id;
                    }else{//专家
                        Yii::app()->session['observation_id'] = '2|'.$people_info->id;
                    }
                }
                $observation_id = Yii::app()->session['observation_id'];
            }
            $array = explode("|",$observation_id);

            $id = intval(Yii::app()->request->getParam('id'));
            $open_info = Open::model()->findByPk($id);

            $data['type']= '';



            $tid = $open_info->tid;
            $gid = explode(",",$open_info->gid);
            $zid = explode(",",$open_info->zid);
            $lid = explode(",",$open_info->lid);
            if(intval($array[0])==1){//老师
                $lsid = $array[1];
                if($lsid==$tid){
                    $data['type'] .= 'ls ';
                }
                if(in_array($lsid,$gid)){//观察者
                    $data['type'] .= 'gc ';
                }
                if(in_array($lsid,$lid)){//听课老师
                    $data['type'] .= 'tk ';
                }
            }elseif(intval($array[0])==2){//专家
                $zjid = $array[1];
                if (in_array($zjid,$zid)){
                    $data['type'] .= 'zj';
                }
            }
            $data['type']  = trim($data['type'],' ');
            if($data['type']){
                $aaa = explode(" ",$data['type']);
            }else{
                $aaa = '';
            }
            if($open_info->end_time < time()) {
                $open_info->status = 2;
                $open_info->update();
                $data['url'] = Yii::app()->createUrl('observation/show', array('id' => $id, 'type' => 'tk', 'openid' => $openid));
                echo json_encode($data);
                exit();
            }
            if($aaa ==''){
                //公开课是否结束
                if($open_info->status==2){
                    $data['url'] = Yii::app()->createUrl('observation/show',array('id'=>$id,'type'=>'tk','openid'=>$openid));
                }else{
                    $data['url'] = '';
                }
            }else{
                $data['url'] = Yii::app()->createUrl('observation/show',array('id'=>$id,'type'=>'tk','openid'=>$openid));
            }
            echo json_encode($data);
            exit();
        }
    }
    public function actionAjaxCheckOpen(){
        $oid = Yii::app()->request->getParam('oid');
        $o = Open::model()->findByPk($oid);

        if($o){
            if($o->status == 0){
                if($o->start_time < time()){
                    $o->status = 1;
                    $o->update();
                    $data['status'] = 1;
                    $data['message'] = '课程开始中';
                    echo json_encode($data);
                    exit();
                }
            }
            if($o->status == 1){
                if($o->end_time < time()){
                    $o->status = 2;
                    $o->update();
                    $data['status'] = 2;
                    $data['message'] = '课程已经结束';
                    echo json_encode($data);
                    exit();
                }
            }

            if($o->status ==1) {
                $data['status'] = 1;
                $data['message'] = '课程开始中';
                echo json_encode($data);
                exit();
            }elseif($o->status ==2){
                $data['status'] = 2;
                $data['message'] = '课程已经结束';
                echo json_encode($data);
                exit();
            }else{
                $data['status']=0;
                $data['message']='课程还没有开始';
                echo json_encode($data);
                exit();
            }
        }else{
            $data['status']=0;
            $data['message']='课程还没有开始';
            echo json_encode($data);
            exit();
        }
    }
    public function actionAjaxOpenOrder(){
        $uid = Yii::app()->request->getParam('uid');
        if($uid){
            $info = OpenOrder::model()->findByPk($uid);
            if($info){
                $info->num = $info->num+1;
                $info->update();
                $data['status']=1;
                $data['num']=$info->num;
                echo json_encode($data);
                exit();
            }
        }else{
            $x = Yii::app()->request->getParam('x');
            $y = Yii::app()->request->getParam('y');
            $type = Yii::app()->request->getParam('type');
            $oid = Yii::app()->request->getParam('oid');
            $info = OpenOrder::model()->find('type=:type and x=:x and y=:y and oid=:oid',array(
                ':type'=>$type,
                ':x'=>$x,
                ':y'=>$y,
                ':oid'=>$oid
            ));
            if($info){
                $info->num = $info->num+1;
                $info->update();
                $data['status']=1;
                $data['num']=$info->num;
                echo json_encode($data);
                exit();
            }else{
                $db = new OpenOrder();
                $db->oid = $oid;
                $db->uid = '';
                $db->user_name = '';
                $db->type = $type;
                $db->num = 1;
                $db->x = $x;
                $db->y = $y;
                $db->create_time = time();
                $db->save();
                $data['status']=2;
                $data['num']=1;
                echo json_encode($data);
                exit();
            }
        }
    }
    public function actionAjaxOpenStart(){
        $oid = Yii::app()->request->getParam('oid');
        $o = Open::model()->findByPk($oid);
        $o->status = 1;
        $ok = $o->update();
        if($ok){
            $data['status']=1;
            $data['message']='开始成功';
            echo json_encode($data);
            exit();
        }else{
            $data['status']=0;
            $data['message']='开始失败';
            echo json_encode($data);
            exit();
        }
    }
    public function actionAjaxOpenEnd(){
        $oid = Yii::app()->request->getParam('oid');
        $o = Open::model()->findByPk($oid);
        $o->status = 2;
        $ok = $o->update();
        if($ok){
            $data['status']=1;
            $data['message']='结束成功';
            echo json_encode($data);
            exit();
        }else{
            $data['status']=0;
            $data['message']='结束失败';
            echo json_encode($data);
            exit();
        }
    }
    public function actionAjaxSelectStudent(){
        $oid = Yii::app()->request->getParam('oid');
        $sid = Yii::app()->request->getParam('sid');
        OpenOrder::model()->deleteAll('oid=:oid',array(':oid'=>$oid));
        $ls_open = OpenOrder::model()->findAll('oid=:oid',array(':oid'=>$sid));
        foreach($ls_open as $v){
            $model = new OpenOrder();
            $model->oid = $oid;
            $model->uid = $v->uid;
            $model->user_name =  $v->user_name;
            $model->type =  $v->type;
            $model->num = '';
            $model->x = $v->x;
            $model->y = $v->y;
            $model->create_time = time();
            $ok = $model->save();
        }
        $data['status']=1;
        $data['message']='加载成功';
        echo json_encode($data);
        exit();
    }
    public function actionAjaxOpenRunning(){
        $type = Yii::app()->request->getParam('type');
        $oid = Yii::app()->request->getParam('oid');
        $running = Yii::app()->request->getParam('running');
        $db = new OpenRunning();
        $db->oid = $oid;
        $db->running = $running;
        $db->type = $type;
        $db->create_time = time();
        $ok = $db->save();
        if($ok){
            $data['status']=1;
            $data['message']='记录成功';
            echo json_encode($data);
            exit();
        }else{
            $data['status']=0;
            $data['message']='记录失败';
            echo json_encode($data);
            exit();
        }
    }
    public function actionAjaxStudent(){
        $oid = Yii::app()->request->getParam('oid');
        $x = Yii::app()->request->getParam('x');
        $y = Yii::app()->request->getParam('y');
        $submit_left_id = Yii::app()->request->getParam('submit_left_id');
        $submit_left_name = Yii::app()->request->getParam('submit_left_name');
        $data = array();
        $data['status']=0;
        $data['left_name'] = '';
        $data['left_id'] = '';
        $data['message']='';
        if($submit_left_name){
            if($submit_left_id){
                $left_db = OpenOrder::model()->findByPk($submit_left_id);
                $left_db->user_name =$submit_left_name ;
                $left_db->update();
                $data['status']=1;
                $data['left_name'] = $submit_left_name;
                $data['left_id'] = $left_db->id;
                $data['message']='添加成功';
            }else{
                $model = new OpenOrder();
                $model->oid = $oid;
                $model->uid = '';
                $model->user_name = $submit_left_name;
                $model->type = 'left';
                $model->x = $x;
                $model->y = $y;
                $model->create_time = time();
                $ok = $model->save();
                if($ok){
                    $data['status']=1;
                    $data['left_name'] = $submit_left_name;
                    $data['left_id'] = $model->attributes['id'];
                    $data['message']='添加成功';
                }else{
                    $data['status']=0;
                    $data['message']='添加失败';
                }
            }
        }
        echo json_encode($data);
        exit();
    }
    public function actionAjaxSendStart(){
        $oid = Yii::app()->request->getParam('oid');
        $openid = Yii::app()->request->getParam('openid');
        $criteria=new CDbCriteria;
        $criteria->condition='pid !=:pid';
        $criteria->params= array(':pid'=>0);
        $criteria->order = 'listorder desc,id asc';
        $allChildPoint = OpenPoint::model()->findAll($criteria);
        $observation_id = Yii::app()->session['observation_id'];
        if(!$observation_id){
            $people_info = Human::model()->find('openid=:openid and is_login=:is_login',
                array(':openid'=>$openid,':is_login'=>1));
            if($people_info){
                //存在
                if($people_info->role = 2){//老师
                    Yii::app()->session['observation_id'] = '1|'.$people_info->id;
                }else{//专家
                    Yii::app()->session['observation_id'] = '2|'.$people_info->id;
                }
            }
            $observation_id = Yii::app()->session['observation_id'];
        }
        $array = explode("|",$observation_id);
        $open_info = Open::model()->findByPk($oid);
        $data['type']= '';
        $tid = $open_info->tid;
        $gid = explode(",",$open_info->gid);
        $zid = explode(",",$open_info->zid);
        $lid = explode(",",$open_info->lid);
        if(intval($array[0])==1){//老师
            $lsid = $array[1];
            if($lsid==$tid){
                $data['type'] .= 'ls ';
            }
            if(in_array($lsid,$gid)){//观察者
                $data['type'] .= 'gc ';
            }
            if(in_array($lsid,$lid)){//听课老师
                $data['type'] .= 'tk ';
            }
        }elseif(intval($array[0])==2){//专家
            $zjid = $array[1];
            if($zid[0] !=''){
                $zjid = $array[1];
                if (in_array($zjid,$zid)){
                    $data['type'] .= 'zj';
                }
            }else{
                $data['type'] .= 'zj';
            }
        }
        $data['type']  = trim($data['type'],' ');
        if($data['type']){
            $aaa = explode(" ",$data['type']);
        }else{
            $aaa = '';
        }


        if($data['type']=='zj'){
            //是否签到
            $os = OpenSignin::model()->find('oid=:oid and type=:type and uid=:uid',array(':oid'=>$oid,':type'=>'zj',':uid'=>$array[1]));
            if($os){
                foreach($allChildPoint as $v){
                    $db = new OpenStart();
                    $db->oid = $oid;
                    $db->pid = $v->pid;
                    $db->type = $array[0];
                    $db->uid = $array[1];
                    $db->opid = $v->id;
                    $db->start =  intval(Yii::app()->request->getParam($v->id));
                    $db->create_time = time();
                    $db->save();
                }
                $data['status']=1;
                $data['message']='提交成功';
                echo json_encode($data);
                exit();
            }else{
                $data['status']=0;
                $data['message']='没有签到不允许评价！';
                echo json_encode($data);
                exit();
            }
        }else{
            //是否签到
            $os = OpenSignin::model()->find('oid=:oid and type=:type and uid=:uid',array(':oid'=>$oid,':type'=>'tk',':uid'=>$array[1]));
            if($os){
                foreach($allChildPoint as $v){
                    $db = new OpenStart();
                    $db->oid = $oid;
                    $db->pid = $v->pid;
                    $db->type = $array[0];
                    $db->uid = $array[1];
                    $db->opid = $v->id;
                    $db->start =  intval(Yii::app()->request->getParam($v->id));
                    $db->create_time = time();
                    $db->save();
                }
                $data['status']=1;
                $data['message']='提交成功';
                echo json_encode($data);
                exit();
            }else{
                $data['status']=0;
                $data['message']='没有签到不允许评价！';
                echo json_encode($data);
                exit();
            }
        }
    }





























































    public function actionClearSession(){
        Yii::app()->session->clear();
    }









    public function actionAjaxSendComment(){
        $oid = Yii::app()->request->getParam('oid');
        $comment = Yii::app()->request->getParam('comment');
        $observation_id = Yii::app()->session['observation_id'];
        $openid = Yii::app()->request->getParam('openid');
        if(!$observation_id){
            $people_info = Human::model()->find('openid=:openid and is_login=:is_login',
                array(':openid'=>$openid,':is_login'=>1));
            if($people_info){
                //存在
                if($people_info->role = 2){//老师
                    Yii::app()->session['observation_id'] = '1|'.$people_info->id;
                }else{//专家
                    Yii::app()->session['observation_id'] = '2|'.$people_info->id;
                }
            }
            $observation_id = Yii::app()->session['observation_id'];
        }
        $array = explode("|",$observation_id);
        $open_info = Open::model()->findByPk($oid);
        $data['type']= '';
        $tid = $open_info->tid;
        $gid = explode(",",$open_info->gid);
        $zid = explode(",",$open_info->zid);
        $lid = explode(",",$open_info->lid);
        if(intval($array[0])==1){//老师
            $lsid = $array[1];
            if($lsid==$tid){
                $data['type'] .= 'ls ';
            }
            if(in_array($lsid,$gid)){//观察者
                $data['type'] .= 'gc ';
            }
            if(in_array($lsid,$lid)){//听课老师
                $data['type'] .= 'tk ';
            }
        }elseif(intval($array[0])==2){//专家
            $zjid = $array[1];
            if($zid[0] !=''){
                $zjid = $array[1];
                if (in_array($zjid,$zid)){
                    $data['type'] .= 'zj';
                }
            }else{
                $data['type'] .= 'zj';
            }
        }
        $data['type']  = trim($data['type'],' ');
        if($data['type']){
            $aaa = explode(" ",$data['type']);
        }else{
            $aaa = '';
        }






        if($data['type']=='zj'){
            $os = OpenSignin::model()->find('oid=:oid and type=:type and uid=:uid',array(':oid'=>$oid,':type'=>'zj',':uid'=>$array[1]));
            if($os){
                $model = new OpenComment();
                $model->oid = $oid;
                $model->type = $array[0];
                $model->uid = $array[1];
                $model->text = $comment;
                $model->create_time = time();
                $ok = $model->save();
                if($ok){
                    $data['status']=1;
                    $data['message']='提交成功';
                    echo json_encode($data);
                    exit();
                }else{
                    $data['status']=0;
                    $data['message']='提交失败';
                    echo json_encode($data);
                    exit();
                }
            }else{
                $data['status']=0;
                $data['message']='没有签到不允许评价！';
                echo json_encode($data);
                exit();
            }
        }else{
            //是否签到
            $os = OpenSignin::model()->find('oid=:oid and type=:type and uid=:uid',array(':oid'=>$oid,':type'=>'tk',':uid'=>$array[1]));
            if($os){
                $model = new OpenComment();
                $model->oid = $oid;
                $model->type = $array[0];
                $model->uid = $array[1];
                $model->text = $comment;
                $model->create_time = time();
                $ok = $model->save();
                if($ok){
                    $data['status']=1;
                    $data['message']='提交成功';
                    echo json_encode($data);
                    exit();
                }else{
                    $data['status']=0;
                    $data['message']='提交失败';
                    echo json_encode($data);
                    exit();
                }
            }else{
                $data['status']=0;
                $data['message']='没有签到不允许评价！';
                echo json_encode($data);
                exit();
            }
        }
    }



















































































































































    public function randStr($length=10){
        $hash='';
        $chars= '0123456789';   //@#!~?:-
        $max=strlen($chars)-1;
        mt_srand((double)microtime()*1000000);
        for($i=0;$i<$length;$i++){
            $hash.=$chars[mt_rand(0,$max)];
        }
        return $hash;
    }
    public function actionLoadClass(){
        $gid = Yii::app()->request->getParam('gid');
        $class = Classes::model()->findAll('gid=:gid and status=:status and is_show=:is_show',array(':gid'=>$gid,':status'=>1,':is_show'=>1));
        $str = '';
        if($class){
            foreach($class as $v){
                $str .= '<option value="'.$v->id.'">'.$v->title.'</option>';
            }
        }
        echo $str;
    }
    public function actionGetApp(){

        $url = Yii::app()->request->getParam('url');
        $app_id = Yii::app()->request->getParam('app_id');
        $app_secret = Yii::app()->request->getParam('app_secret');
        $con = Yii::app()->request->getParam('con');
        echo $result_config_data = XUtils::getSignPackage($app_id,$app_secret,$url,$con);
    }
    public function timeRunning($time){
        $milliseconds = floor(($time % 1000) / 100);
        $seconds = floor(($time/1000) % 60);
        $minutes = floor(($time/(1000*60)) % 60);
        $hours = floor(($time/(1000*60*60)) % 24);
        $seconds = $seconds < 10 ? '0' + $seconds : $seconds;
        $minutes = $minutes < 10 ? '0' + $minutes : $minutes;
        return $hours .":" .$minutes . ":" . $seconds . "." . $milliseconds;
    }
    public function getfirstchar($s0){
        $firstchar_ord=ord(strtoupper($s0{0}));
        if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57)) return $s0{0};
        $s=iconv("UTF-8","gb2312", $s0);
        $asc=ord($s{0})*256+ord($s{1})-65536;
        if($asc>=-20319 and $asc<=-20284)return "A";
        if($asc>=-20283 and $asc<=-19776)return "B";
        if($asc>=-19775 and $asc<=-19219)return "C";
        if($asc>=-19218 and $asc<=-18711)return "D";
        if($asc>=-18710 and $asc<=-18527)return "E";
        if($asc>=-18526 and $asc<=-18240)return "F";
        if($asc>=-18239 and $asc<=-17923)return "G";
        if($asc>=-17922 and $asc<=-17418)return "H";
        if($asc>=-17417 and $asc<=-16475)return "J";
        if($asc>=-16474 and $asc<=-16213)return "K";
        if($asc>=-16212 and $asc<=-15641)return "L";
        if($asc>=-15640 and $asc<=-15166)return "M";
        if($asc>=-15165 and $asc<=-14923)return "N";
        if($asc>=-14922 and $asc<=-14915)return "O";
        if($asc>=-14914 and $asc<=-14631)return "P";
        if($asc>=-14630 and $asc<=-14150)return "Q";
        if($asc>=-14149 and $asc<=-14091)return "R";
        if($asc>=-14090 and $asc<=-13319)return "S";
        if($asc>=-13318 and $asc<=-12839)return "T";
        if($asc>=-12838 and $asc<=-12557)return "W";
        if($asc>=-12556 and $asc<=-11848)return "X";
        if($asc>=-11847 and $asc<=-11056)return "Y";
        if($asc>=-11055 and $asc<=-10247)return "Z";
        return null;
    }
    public function actionZimu(){
        $aaa = People::model()->findAll();
        foreach($aaa as $k=>$v){
            $v->zimu = $this->getfirstchar($v->user_name);
            $v->update();
        }
    }

}